package com.dianrui.fmi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.bean.HotCommunity;
import com.dianrui.fmi.interfaces.NineAdapter2OperationCallBack;
import com.dianrui.fmi.util.GlideUtil;
import com.dianrui.fmi.view.InnerGridView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HotCommentuntyAdapter extends BaseAdapter {
    private List<HotCommunity> list;
    private HotCommentuntyClickListener hotCommentuntyClickListener;
    private NineAdapter2OperationCallBack nineAdapter2OperationCallBack;
    private Context context;

    public HotCommentuntyAdapter(Context context, List<HotCommunity> list,NineAdapter2OperationCallBack nineAdapter2OperationCallBack) {
        this.list = list;
        this.context = context;
        this.nineAdapter2OperationCallBack = nineAdapter2OperationCallBack;
    }

    public void refresh(List<HotCommunity> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface HotCommentuntyClickListener {
        void ClickMain(String community_id);

        void reply(String communityId,String memberId);

        void share();

        void like(int pos,String community_id, ImageView imageView);

    }

    public void setOnHotCommentuntyClickListener(HotCommentuntyClickListener clickListener) {
        this.hotCommentuntyClickListener = clickListener;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_community_hot, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }

        if (list != null && list.size() > 0) {
            final HotCommunity hotCommunity = list.get(position);
            GlideUtil.loadUserImageViewSize(parent.getContext(),hotCommunity.face,holder.face);
            holder.nickname.setText(hotCommunity.nickname);
            holder.createTime.setText(hotCommunity.create_time);
            holder.contents.setText(hotCommunity.contents);
            holder.commentNumber.setText(hotCommunity.comment_number);
            holder.collectNumber.setText(hotCommunity.share_number);
            holder.isCollect.setText(hotCommunity.collect_number);
            if (hotCommunity.is_collect==1) {
                holder.likeImg.setImageResource(R.mipmap.collected);
            }else if (hotCommunity.is_collect==0) {
                holder.likeImg.setImageResource(R.mipmap.uncollected);
            }
            if (hotCommunity.picture.size()>0) {
                holder.innerGridView.setAdapter(new NineAdapter(hotCommunity.picture,nineAdapter2OperationCallBack));
                holder.innerGridView.setVisibility(View.VISIBLE);
            }else {
                holder.innerGridView.setVisibility(View.GONE);
            }
            holder.replyLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (hotCommentuntyClickListener != null) {
                        hotCommentuntyClickListener.reply(hotCommunity.community_id, hotCommunity.member_id);
                    }
                }
            });
            holder.likeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (hotCommentuntyClickListener != null) {
                        hotCommentuntyClickListener.like(position, hotCommunity.community_id, holder.likeImg);
                    }
                }
            });

            holder.shareLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (hotCommentuntyClickListener != null) {
                        hotCommentuntyClickListener.share();
                    }
                }
            });

            holder.clickCommunityMain.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (hotCommentuntyClickListener != null) {
                        hotCommentuntyClickListener.ClickMain(hotCommunity.community_id);
                    }
                }
            });

            holder.innerGridView.setOnTouchInvalidPositionListener(new InnerGridView.OnTouchInvalidPositionListener() {
                @Override
                public boolean onTouchInvalidPosition(int motionEvent) {
                    if (hotCommentuntyClickListener != null) {
                        hotCommentuntyClickListener.ClickMain(hotCommunity.community_id);
                    }
                    return false;
                }
            });
        }


        return convertView;
    }


     class ViewHolder {
        @BindView(R.id.face)
        ImageView face;
        @BindView(R.id.nickname)
        TextView nickname;
        @BindView(R.id.create_time)
        TextView createTime;
        @BindView(R.id.contents)
        TextView contents;
        @BindView(R.id.comment_number)
        TextView commentNumber;
        @BindView(R.id.reply_layout)
        LinearLayout replyLayout;
        @BindView(R.id.collect_number)
        TextView collectNumber;
        @BindView(R.id.share_layout)
        LinearLayout shareLayout;
        @BindView(R.id.like_img)
        ImageView likeImg;
        @BindView(R.id.is_collect)
        TextView isCollect;
        @BindView(R.id.nine_view)
        InnerGridView innerGridView;
        @BindView(R.id.like_layout)
        LinearLayout likeLayout;
        @BindView(R.id.click_community_main)
        LinearLayout clickCommunityMain;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
